package com.ruoyi.project.rate;

import java.util.Comparator;

import org.apache.commons.lang3.StringUtils;

import com.ruoyi.project.rate.domain.Rule;

public class RuleComparator implements Comparator<Rule> {
	@Override
	public int compare(Rule o1, Rule o2) {
		if (!StringUtils.equals(o1.getKhz(), o2.getKhz())) {
			return o1.getKhz().compareTo(o2.getKhz());
		}
		if (!StringUtils.equals(o1.getType(), o2.getType())) {
			return o1.getType().compareTo(o2.getType());
		}
		
		String xh1 = StringUtils.defaultIfBlank(o1.getXh(), "");
		String xh2 = StringUtils.defaultIfBlank(o2.getXh(), "");
		
		if (xh1.contains("-") && xh2.contains("-")) {
			String[] _xh1 = xh1.split("-");
			String[] _xh2 = xh2.split("-");
			if (_xh1.length == 3 && _xh2.length == 3) {
				String xh10 = _xh1[0].trim();
				String xh11 = _xh1[1].trim();
				String xh12 = _xh1[2].trim();
				String xh20 = _xh2[0].trim();
				String xh21 = _xh2[1].trim();
				String xh22 = _xh2[2].trim();
				if (xh10.equals(xh20)) {
					if (xh11.equals(xh21)) {
						return new Integer(xh12).compareTo(new Integer(xh22));
					} else {
						return new Integer(xh11).compareTo(new Integer(xh21));
					}
				} else {
					return new Integer(xh10).compareTo(new Integer(xh20));
				}
			}
		}
		
		return xh1.compareTo(xh2);
	}
}
